<?php

class helper {
    public static function encrypt($str,$key) {
        // very simple encryptor with key
        $result = '';
        for ($i=0; $i<strlen($str); $i++) {
            $char = substr($str,$i,1);
            $keychar = substr($key,($i%strlen($key))-1,1);
            $char = chr(ord($char)+ord($keychar));
            $result .= $char;
        }
        return urlencode(base64_encode($result));
    }
    
    public static function digitisasi($int) {
		$int = str_split((string)$int);
		$i = 0;
		$retval = "";
		for ($j = count($int) - 1; $j >= 0; $j--) {
			if ($i == 3) {
				$retval = $int[$j].".".$retval;
				$i = 1;
			} else {
				$retval = $int[$j].$retval;
				$i++;
			}		
		}
		//$retval = "Rp ".$retval;
		return $retval;
	}
    
    public static function decrypt($str,$key) {
        // decryptor of raing_encrypt, use the same key with raing_encrypt
        // $str = urldecode($str); // uncomment if failed to decrypt
        $str = base64_decode($str);
        $result = '';
        for ($i = 0; $i < strlen($str); $i++) {
            $char = substr($str,$i,1);
            $keychar = substr($key,($i%strlen($key))-1,1);
            $char = chr(ord($char)-ord($keychar));
            $result .= $char;
        }
        return $result;
    }
    
    public static function trimMsg($msg,$length){
		$text = "";
		$panjang = $length-3;
		$text = (strlen($msg) > $length) ? substr($msg,0,$panjang).'...' : $msg;
		return $text;
	}
    
    function kirimSMS($ukey,$pkey,$nomor,$pesan){
		$url = 'http://zenziva.com/apps/smsapi.php?userkey='.$ukey.'&passkey='.$pkey.'&nohp='.$nomor.'&pesan='.urlencode($pesan);
		$curlHandle = curl_init();
		curl_setopt($curlHandle, CURLOPT_URL, $url);
		//curl_setopt($curlHandle, CURLOPT_HEADER, 0);
		curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Codular Sample cURL Request');
		curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
		$results = curl_exec($curlHandle);
		curl_close($curlHandle);
	}
    
    public static function i2c_realip() {
        // No IP found (will be overwritten by for
        // if any IP is found behind a firewall)
        $ip = FALSE;
       
        // If HTTP_CLIENT_IP is set, then give it priority
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
       
        // User is behind a proxy and check that we discard RFC1918 IP addresses
        // if they are behind a proxy then only figure out which IP belongs to the
        // user.  Might not need any more hackin if there is a squid reverse proxy
        // infront of apache.
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

            // Put the IP's into an array which we shall work with shortly.
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }

            for ($i = 0; $i < count($ips); $i++) {
                // Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and
                // 192.168.0.0/16
                if (!preg_match('/^(?:10|172\.(?:1[6-9]|2\d|3[01])|192\.168)\./', $ips[$i])) {
                    if (version_compare(phpversion(), "5.0.0", ">=")) {
                        if (ip2long($ips[$i]) != false) {
                            $ip = $ips[$i];
                            break;
                        }
                    } else {
                        if (ip2long($ips[$i]) != -1) {
                            $ip = $ips[$i];
                            break;
                        }
                    }
                }
            }
        }

        // Return with the found IP or the remote address
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }
    
    public static function inputing($string,$remove_new_line = true) {
        if ($remove_new_line) {
            return trim(preg_replace(array('/\n+|\r+/'),array(''),stripslashes($string)));
        } else {
            return trim(stripslashes($string));
        }
    }
    
    public static function setcookie($name,$value,$time) {
		// for setting cookie that different domain in development and release or so on
		setcookie($name,$value,time()+$time,'/',COOKIE_DOMAIN);
	}
	
	public static function time_ago($date) {
		$time_dia = strtotime($date);
		$time_now = time();
		$time_diff = $time_now - $time_dia;
		if ($time_diff < 60) {
			return 'baru saja';
		} elseif ($time_diff < 3600) {
			return floor($time_diff/60).' menit yang lalu';
		} elseif ($time_diff < 86400) {
			return floor($time_diff/3600).' jam yang lalu';
		} elseif ($time_diff < 2592000) {
			return floor($time_diff/86400).' hari yang lalu';
		} elseif ($time_diff < 7776000) {
			return floor($time_diff/2592000).' bulan yang lalu';
		} else {
			return 'udah lama';
		}
	}
	
	public static function formDate($date) {
		$date = new DateTime($date);
		$result = $date->format('d/m/Y H:i');
		return $result;
	}
	
	public static function slugging($string) {
		$nstring = preg_replace("/[^a-zA-Z0-9]/","-",strtolower($string));
		return $nstring;
	}
    
    public static function page() {
        $page = isset($_GET['page']) ? ((intval($_GET['page']) > 0) ? intval($_GET['page']) : 1) : 1;
        return $page;
    }
}
